package com.nf.health.app.inquiry.adapter;

import android.app.Activity;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hyphenate.easeui.widget.CircularImage;
import com.nf.health.app.R;
import com.nf.health.app.customview.MyFlowLayout;
import com.nf.health.app.models.DoctorCommnetVo;
import com.nf.health.app.utils.DateUtil;
import com.nf.health.app.utils.ImageUtils;
import com.nf.health.app.widget.OnMyOnClistener;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.List;

/**
 * Created by Jason on 2016/9/28.
 */
public class DoctorCommentListAdapter extends BaseAdapter{
    private int CPOUNT_ITEM_NUM = 0;
    private LayoutInflater inflater;
    private Activity cnt ;
    private OnMyOnClistener onMyOnClistener;//注册点击事件

    public List<DoctorCommnetVo>  datas;


    public DoctorCommentListAdapter(Activity cnt, List<DoctorCommnetVo> datas){
        inflater =LayoutInflater.from(cnt);
        this.cnt = cnt;
        this.datas = datas;
    }
    public void setDoctorCommentData(List<DoctorCommnetVo> datas){
        this.datas = datas;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView==null) {
            convertView = View.inflate(cnt, R.layout.view_comment_list, null);
            holder = new ViewHolder();
            holder.iv_img_discuss = (CircularImage) convertView.findViewById(R.id.iv_img_discuss);
            holder.tv_discuss_doc_name = (TextView) convertView.findViewById(R.id.tv_discuss_doc_name);
            holder.tv_is_satisfied = (TextView) convertView.findViewById(R.id.tv_is_satisfied);
            holder.tv_discuss_doc_time = (TextView) convertView.findViewById(R.id.tv_discuss_doc_time);
            holder.mFlowLayout  = (MyFlowLayout) convertView.findViewById(R.id.mygv_status_satify);
            holder.tv_word_discuss  = (TextView) convertView.findViewById(R.id.tv_word_discuss);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final  DoctorCommnetVo searchMainDocBean   = datas.get(position);
        if(searchMainDocBean == null){
            return convertView;
        }
        DoctorCommnetVo doctorCommnetVo = datas.get(position);
        reFreshMyTvText(doctorCommnetVo,holder);
        return convertView;
    }

    static class ViewHolder{
        CircularImage iv_img_discuss;//医生的头像
        //医生姓名
        TextView tv_discuss_doc_name;
        //满意程度
        TextView tv_is_satisfied;
        //存放GidView的满意程度
        MyFlowLayout mFlowLayout;
        //评论的时间
        TextView tv_discuss_doc_time;
        //医生评论
        TextView tv_word_discuss;

    }
    /**
     * 刷新布局
     */
    public void   reFreshMyTvText(DoctorCommnetVo doctorCommnetVo,ViewHolder holder){
        if(doctorCommnetVo == null){
            return ;
        }
        Glide.with(cnt).load(doctorCommnetVo.getHimg()).placeholder(R.drawable.default_head).into(holder.iv_img_discuss);
        holder.tv_discuss_doc_name.setText(TextUtils.isEmpty(doctorCommnetVo.getNick()) ? "" : doctorCommnetVo.getNick());
        holder.tv_is_satisfied.setText(TextUtils.isEmpty(doctorCommnetVo.getNick()) ? "" : doctorCommnetVo.getNick());
       /* long date2String = DateUtil.getLongToDate(doctorCommnetVo.getLabel(), "yyyy-MM-dd HH:mm:ss");
        String time =  DateUtil.date2String(date2String, "HH:mm"); */
        String  time = DateUtil.getMyDateToSubYearAndMonthAndDay(doctorCommnetVo.getPosttime());
        holder.tv_discuss_doc_time.setText(TextUtils.isEmpty(time) ? "" : time);
        holder.tv_word_discuss.setText(TextUtils.isEmpty(doctorCommnetVo.getMessage()) ? "" : doctorCommnetVo.getMessage());
        String status =  TextUtils.isEmpty(doctorCommnetVo.getScore()) ? "3" : doctorCommnetVo.getScore();
        String is_satisfied1 = "不满意";
        String is_satisfied2 = "一般";
        String is_satisfied3 = "满意";
        if(status.equals("1")){//不满意
            holder.tv_is_satisfied.setText(is_satisfied1);
            ImageUtils.setTextViewBg(holder.tv_is_satisfied, cnt, R.drawable.shade_full_oval_orange, is_satisfied1);
        }else if(status.equals("2")){//一般
            holder.tv_is_satisfied.setText(is_satisfied2);
            ImageUtils.setTextViewBg(holder.tv_is_satisfied, cnt, R.drawable.shade_full_oval_blue, is_satisfied2);
        }else if(status.equals("3")){//满意
            holder.tv_is_satisfied.setText(is_satisfied3);
            ImageUtils.setTextViewBg(holder.tv_is_satisfied, cnt, R.drawable.shade_full_oval, is_satisfied3);
        }
        String label = doctorCommnetVo.getLabel();
        holder.mFlowLayout.removeAllViews();
        if(!TextUtils.isEmpty(label)){
            try {
                JSONArray list = new JSONArray(label);
                if(list !=null){
                    for (int i = 0; i <list.length() ; i++) {
                        TextView textView = new TextView(cnt);
                        ImageUtils.setTextViewBg(textView, cnt, R.drawable.shade_enpety_oval, list.getString(i));
                        ViewGroup.MarginLayoutParams marginLayoutParams =  new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 70);
                        marginLayoutParams.setMargins(15, 0, 15, 0);
                        textView.setGravity(Gravity.CENTER);
                        textView.setMinLines(40);
                        if(status.equals("1")){//不满意
                            ImageUtils.setTextViewBg(textView, cnt, R.drawable.shade_enpety_oval_orange, list.getString(i));
                            textView.setTextColor(cnt.getResources().getColor(R.color.color1));
                        }else if(status.equals("2")){//一般
                            ImageUtils.setTextViewBg(textView, cnt, R.drawable.shade_enpety_oval_blue, list.getString(i));
                            textView.setTextColor(cnt.getResources().getColor(R.color.color_blue));
                        }else if(status.equals("3")){//满意
                            ImageUtils.setTextViewBg(textView, cnt, R.drawable.shade_enpety_oval, list.getString(i));
                            textView.setTextColor(cnt.getResources().getColor(R.color.theme_color));
                        }
                        textView.setTextSize(12);
                        textView.setGravity(Gravity.CENTER);
                        holder.mFlowLayout.addView(textView, marginLayoutParams);
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

}
